//
//  LeftViewController.m
//  QuickCure37
//
//  Created by 马文星 on 16/6/17.
//  Copyright © 2016年 Demos. All rights reserved.
//

#import "XHRPersonalMainPageViewController.h"
#import "XHRPersonalMainCell.h"

#import "JASidePanelController.h"
#import "XHRApplyFamousDoctorViewController.h"
#import "XHRMedicalRecordViewController.h"
#import "XHRPayAttentionViewController.h"
#import "XHRSetAndHelpViewController.h"
#import "XHRAboutProductViewController.h"
#import "XHRShareToFriendsViewController.h"

#import "UIViewController+JASidePanel.h"
#import "JASidePanelController.h"
#import "XHRPersonInfoVC.h"
#import "XHRLoginInfo.h"
#import "XHRLoginController.h"
#import "XHRRegistController.h"
#import "WWSameSickVC.h"


@interface XHRPersonalMainPageViewController ()<UITableViewDelegate,UITableViewDataSource>

/** 没有登录界面 */
@property (nonatomic, weak) UIView *noLoginBGView;

/** 有登录界面 */
@property (nonatomic, weak) UIView *loginBGView;

/** tableView */
@property (nonatomic, weak) UITableView *tableview;

/** 标题数组,里面的小数组是放的是每一个section中的标题内容*/
@property (nonatomic, strong) NSArray <NSArray *>*dataArray;




@end

@implementation XHRPersonalMainPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initSettings];
    
    // 设置通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(call) name:@"cancle" object:nil];
}

// 通知调用的方法
- (void)call{
    self.login = NO;
}

#pragma mark  - 配置
-(void)initSettings{
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.hidden = YES;
    
    self.dataArray = @[@[@"名医通申请",@"病例管理",@"关注医生"],@[@"设置与帮助",@"关于产品",@"分享给好友"]];
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        
        UIView *view = nil;
        
        // 拦截判断用户是否已经登录
        if (self.login) {
            view = self.loginBGView;
            
        } else{
            view = self.noLoginBGView;
        }
        
        make.top.mas_equalTo(view.mas_bottom);
        make.left.bottom.mas_equalTo(0);
        make.width.mas_equalTo(XHRScreenWidth * 0.8);
    }];
}

#pragma mark  - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray[section].count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    XHRPersonalMainCell *cell = [XHRPersonalMainCell personalMainCellWithTableView:tableView];
    cell.textLabel.text = self.dataArray[indexPath.section][indexPath.row];

    return cell;
}
#pragma mark  - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return 0.01;
    }
    return 5;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NSInteger section = indexPath.section;
    NSInteger row     = indexPath.row;
    //1.创建一个空的，下面重新赋值
    UIViewController *newVC = nil;
    if (section==0) {
        switch (row) {
            case 0:
                newVC = [[XHRApplyFamousDoctorViewController alloc] init];
                break;
            case 1:
                newVC = [[XHRMedicalRecordViewController alloc] init];
                break;
            case 2:
                newVC = [[XHRPayAttentionViewController alloc] init];
                break;
            default:
                break;
        }
    }else{
        
        switch (row) {
            case 0:
                
                newVC = [[XHRSetAndHelpViewController alloc] init];
                break;
                
            case 1:
                
                newVC = [[XHRAboutProductViewController alloc] init];
                break;
                
            case 2:
                
                newVC = [[XHRShareToFriendsViewController alloc] init];
                break;
            default:
                break;
        }
   }
    newVC.navigationItem.title = self.dataArray[section][row];

    [self pushToNewViewController:newVC];
}
#pragma mark -
#pragma mark - 从左侧跳转到对应的控制器

- (void)pushToNewViewController:(UIViewController *)newVC
{
    JASidePanelController *viewController = (JASidePanelController *)XHRKeyWindow.rootViewController;
    [viewController showCenterPanelAnimated:YES];
    UINavigationController *nav = (UINavigationController *)viewController.centerPanel;
    [nav pushViewController:newVC animated:YES];
}



#pragma mark -
#pragma mark - 懒加载
- (UITableView *)tableview{
    
    if (!_tableview) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [self.view addSubview:tableView];
        _tableview = tableView;
        _tableview.dataSource = self;
        _tableview.delegate = self;
    }
    return _tableview;
}


-(UIView *)noLoginBGView{
    
    if (_noLoginBGView == nil) {
        UIView *noLoginBGView  = [[UIView alloc] initWithFrame:CGRectMake(0, 0,XHRScreenWidth , 120)];
        _noLoginBGView = noLoginBGView;
        [self.view addSubview:noLoginBGView];
        _noLoginBGView.backgroundColor = XHRThemeColor;
        
        //头像
        UIImageView *iconV = [[UIImageView alloc] init];
        iconV.image = [UIImage imageNamed:@"name"];
        [_noLoginBGView addSubview:iconV];
        iconV.bounds = CGRectMake(0, 0, 45, 45);
        iconV.center = CGPointMake(XHRScreenWidth * 0.8 / 2, 50);
        
        
        //登录
        UIButton *leftButton = [[UIButton alloc] init];
        [_noLoginBGView addSubview:leftButton];
        [leftButton setTitle:@"登录" forState:UIControlStateNormal];
        [leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [leftButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        
        [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(iconV.mas_bottom).offset(10);
            make.right.mas_equalTo(iconV.mas_centerX).offset(-10);
            make.size.mas_equalTo(CGSizeMake(100, 30));
        }];
        
        //注册
        UIButton *rightButton = [[UIButton alloc] init];
        [_noLoginBGView addSubview:rightButton];
        [rightButton setTitle:@"注册" forState:UIControlStateNormal];
        [rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [rightButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(iconV.mas_bottom).offset(10);
            make.left.mas_equalTo(iconV.mas_centerX).offset(10);
            make.size.mas_equalTo(CGSizeMake(100, 30));
        }];
        
    }

    return _noLoginBGView;
}

-(UIView *)loginBGView{
    
    if (_loginBGView == nil) {
        UIView *loginBGView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,XHRScreenWidth , 120)];
        _loginBGView = loginBGView;
        [self.view addSubview:loginBGView];
        _loginBGView.backgroundColor = XHRThemeColor;
        
        //头像
        UIImageView *iconV = [[UIImageView alloc] init];
        iconV.image = [UIImage imageNamed:@"banner"];
        [_loginBGView addSubview:iconV];
        iconV.layer.cornerRadius = 35;
        iconV.layer.masksToBounds = YES;
        
        [iconV mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.size.mas_equalTo(CGSizeMake(70, 70));
            make.left.mas_equalTo(20);
            make.top.mas_equalTo(30);
        }];
        
        // 为iconV添加tap
        iconV.userInteractionEnabled = YES;

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(checkPersonInfo)];

        [iconV addGestureRecognizer:tap];
        
        //昵称
        UILabel *nameLabel = [[UILabel alloc] init];
        nameLabel.text = @"王二才";
        nameLabel.font = [UIFont systemFontOfSize:16];
        [_loginBGView addSubview:nameLabel];
        
        [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(iconV).offset(5);
            make.left.mas_equalTo(iconV.mas_right).offset(15);
            
        }];
        
        //个人资料
        UILabel *descLabel = [[UILabel alloc] init];
        descLabel.text = @"查看个人资料";
        descLabel.font = [UIFont systemFontOfSize:15];
        [_loginBGView addSubview:descLabel];
        [descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.bottom.equalTo(iconV).offset(-5);
            make.left.mas_equalTo(iconV.mas_right).offset(15);
            
        }];
        
        // label添加点按事件
        descLabel.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(checkPersonInfo)];
        [descLabel addGestureRecognizer:tap2];
        
    }
    return _loginBGView;
}

#pragma
#pragma mark -查看个人信息
- (void)checkPersonInfo{
    XHRPersonInfoVC *vc = [XHRPersonInfoVC new];
    
    [self pushToNewViewController:vc];
}


#pragma mark - 跳转到登录或注册界面
-(void)clickButton:(UIButton *)sender{
    
    if ([sender.titleLabel.text isEqualToString:@"登录"]) {
        // 登录界面
        XHRLoginController *loginVc = [[XHRLoginController alloc] init];
        loginVc.isLoginBlock = ^(BOOL isLogin){
            self.login = isLogin;
        };
        
        [self pushToNewViewController:loginVc];
        
    } else {
        // 注册界面
        XHRRegistController *registVc = [[XHRRegistController alloc] init];
        [self pushToNewViewController:registVc];
    }
}


/** 决定使用login还是nologin **/
-(BOOL)login{
    
    if ([XHRLoginInfo validateIsLogined]){
        
        return YES;
    }
    
    return NO;
}

/** 赋值leftView的用户个人信息 **/
-(void)setLogin:(BOOL)login{
    
    self.loginBGView = nil;
    self.noLoginBGView = nil;
    
    // 重新加载用户信息
    if (login){
        [self loginBGView];
    } else{
        [self noLoginBGView];
    }
}

@end
